<?php

namespace app\home\controller;

use think\Controller;
use app\common\model\Cart as Carts;

class Cart extends Base
{   
    // 添加购物车
    public function addcart(){
        $data=input();
        // dump($data);
        $this::datacart($data['goods_id'],$data['spec_goods_id'],$data['number']);
        // $goods=\app\common\model\Goods::where('id',$data['goods_id'])->find();
        // // dump($goods);
        // $goodSpec=\app\common\model\SpecGoods::where('id',$data['spec_goods_id'])->find();
        // // dump($goodSpec);
        // $res=[
        //     'id'=>$data['goods_id'],
        //     'goods_name'=>$goods['goods_name'],
        //     'goods_logo'=>$goods['goods_logo'],
        //     'value_names'=>$goodSpec['value_names'],
        //     'number'=>$data['number']
        // ];
        // // dump($res);
        $res=$this::getAllCart($data['goods_id'],$data['spec_goods_id'],$data['number']);
        return $this->fetch('success-cart',['cartData'=>$res]);
    }
    //处理添加购物车数据
    public static function datacart($goods_id,$spec_goods_id,$number,$is_selected=1){
        if(session('user_info')){
            $user_id=session('user_info.id');
            // dump($user_id);
            $where=['user_id'=>$user_id,'goods_id'=>$goods_id,'spec_goods_id'=>$spec_goods_id];
            $info=Carts::where($where)->find();
            if($info){
                $info->number=$number;
                $info->is_selected=$is_selected;
                $info->save();
            }else{
                $where['number']=$number;
                $where['is_selected']=$is_selected;
                $data=Carts::create($where);
            }
        }else{
            // dump('no');
            //未登录 数据cookie或者session 或者缓存
            //先从cookie中取出数据
            $data=cookie('cart')?:[];
            //判断是否存在购物记录（商品 id  商品规格id  相同）
            $key=$goods_id."_".$spec_goods_id;
            if(isset($data[$key])){
                //存在相同数据  
                $data[$key]['number']=$number;
                $data[$key]['is_selected']=$is_selected;
            }else{
                //否则  保存数据
                $data[$key]=[
                    'id'=>$key,
                    'goods_id'=>$goods_id,
                    'number'=>$number,
                    'spec_goods_id'=>$spec_goods_id,
                    'is_selected'=>$is_selected
                ];
            }
            //存到cookie
            cookie('cart',$data,24*3000);
        }
    }
    //购物车列表
    public function cart(){
        if(session('user_info')){
            //获取登录用户id
            $user_id=session('user_info.id');
            $cartgoods=Carts::where('user_id',$user_id)->select();
            // dump($cartgoods);
            foreach($cartgoods as $v){
                $res[]=$this::getAllCart($v['goods_id'],$v['spec_goods_id'],$v['number'],$v['is_selected'],$v['id']);
            }
        }
        else{
            //未登录  去cookie里面取
            $data=cookie('cart')?:[];
            //转换成二维数组 ，去掉字符串下标
            $data=array_values($data);
            foreach($data as $v){
                $res[]=$this::getAllCart($v['goods_id'],$v['spec_goods_id'],$v['number'],$v['is_selected'],$v['id']);
            }
        }
        // dump($res); die;           
        return view('cart',['cartlist'=>$res]);
    }

    //查询购物车数据 
    public static function getAllCart($goods_id,$spec_goods_id,$number,$is_selected=1,$id=""){
        $goods=\app\common\model\Goods::where('id',$goods_id)->find();
        // dump($goods);
        $goodSpec=\app\common\model\SpecGoods::where('id',$spec_goods_id)->find();
        // dump($goodSpec);
        $res=[
            'id'=>$id,
            'goods_name'=>$goods['goods_name'],
            'goods_logo'=>$goods['goods_logo'],
            'value_names'=>$goodSpec['value_names'],
            'number'=>$number,
            'price'=>$goodSpec['price'],
            'is_selected'=>$is_selected,
        ];
        // dump($res);
        return $res;    
    }

    
    //将cookie里面的数据迁移到数据库
    public static function cookieToTable(){
        //从cookie中去数据
        $data=cookie('cart')?:[];
        //将数据添加或者修改到数据表
        foreach($data as $v){
            self::datacart($v['goods_id'],$v['spec_goods_id'],$v['number']);
        }
        //删除cookie购物车数据
        cookie('cart',null);
    }


    //处理购物车商品数量
    public function changeNum(){
        // dump(input());
        //接收参数
        $param=input();
        //参数检测
        //处理数据
        if(session('user_info')){
            //登录
            $user_id=session('user_info.id');
            //修改数据表中的数量字段
            Carts::where(['id'=>$param['id'],'user_id'=>$user_id])->update(['number'=>$param['number']]);
        }else{
            //未登录
            //取cookie数据
            $data=cookie('cart')?:[];
            //修改数量
            $data[$param['id']]['number']=$param['number'];
            cookie('cart',$data,24*3600);
        }
        //返回结果
        $res=['code'=>200,'msg'=>'success'];
        json($res)->send();
    }
    //删除购物车数据
    public function deleteCart(){
        //获取参数
        $param=input();
        //参数检测
        //处理数据
        if(session('user_info')){
            //登录
            $user_id=session('user_info.id');
            //修改数据表中的数量字段
            Carts::where(['id'=>$param['id'],'user_id'=>$user_id])->delete();
        }else{
            //未登录
            //取cookie数据
            $data=cookie('cart')?:[];
            //删除一条数据
            unset($data[$param['id']]);
            //重新保存
            cookie('cart',$data,24*3600);
        }
        //返回结果
        $res=['code'=>200,'msg'=>'success'];
        json($res)->send();
    }

}
